 var table ,form ,laypage ,layer ,element ,laydate ,upload//时间组件
$(function(){
	 layui.use(['table','form','laypage','layer', 'element', 'laydate','upload'], function(){
          table = layui.table;
          form=layui.form;
          laypage = layui.laypage ;//分页
          layer = layui.layer ;//弹层
          element = layui.element; //元素操作
          laydate = layui.laydate; //时间组件 
          upload = layui.upload;
          var roleid=getQueryString("id");
          var upUrl="/sysRole/save.do"
  			//上级菜单选择
  		  var setting = {
  					check : {
  						enable : true,
  						chkboxType : {
  							"Y" : "p",
  							"N" : "s"
  						}
  					},
  					view : {
  						dblClickExpand : false
  					},
  					data : {
  						simpleData : {
  							enable : true,
  							idKey : "id",
  							pIdKey : "pid"
  						},
  						key : {
  							url : 'none'
  						}
  					},
  					callback : {
  						beforeClick : permissionBeforeClick,
  						onCheck : permissionOnCheck
  					}
  				};
  			$.ajax({
  		        url: "/sysPermission/getList.do",               //请求地址
  		        type: "POST",              //请求类型
  		        data:{},              //请求数据
  		        async:false,
  		        timeout: 200000,                //请求超时时间(毫秒)
  		        headers: setHeader(),
  		        success:function(res){         //请求成功
  		     	   if(res.status == 200){
  		     		  var zNodes=res.resultData
  		     		  var zTree = $.fn.zTree.init($("#permissionTree"), setting ,zNodes);
  		     		 zTree.expandAll(true);//节点展开
  				   }else if(res.status == 666){
  					  TIMEOUT(); 
  			       }else{
   	  					layer.msg(res.resultMsg, {icon: res.icon});
   	  				} 
  		       }
  		    }) 	  
           if(roleid){
        	   $("#subTitle").html("编辑角色");
        	   upUrl="/sysRole/update.do"
      		   var requestData = {"id": roleid};
	  		    $.ajax({
	  		        url: "/sysRole/selectOne.do",               //请求地址
	  		        type: "POST",              //请求类型
	  		        data:formatParam(requestData),              //请求数据
	  		        timeout: 200000,                //请求超时时间(毫秒)
	  		        headers: setHeader(),
	  		        success:function(res){         //请求成功
	  		     	   if(res.status == 200){
	  		     		   var scmsPower=res.resultData.sysPower;
	  		     		   var scmsRole=res.resultData.sysRole;
	  		     		    $("#id").val(scmsRole.id);
	  			  		    $("#name").val(scmsRole.name);
	  			  		    $("#remarks").val(scmsRole.remarks);	
		  			  		if(scmsPower.length>0){
		         				 //异步加载获取当前用户组拥有的权限列表，然后将拥有的权限菜单项勾选
		         				  var ids="";
		         				  var zt = $.fn.zTree.getZTreeObj("permissionTree");
		         				  for(var i=0;i<scmsPower.length;i++){
		     					     var node = zt.getNodeByParam("id", scmsPower[i].permissionId ,null);
		     				           zt.checkNode(node, true, false);
		     						    ids += (scmsPower[i].permissionId +',');
		         				  };
		         				  ids = ids.substring(0, ids.length-1);
		         				  $('#pid').val(ids);
		         			}
		  			  		form.render();
	  				   }else if(res.status == 666){
	  					  TIMEOUT(); 
	  			       }else{
	   	  					layer.msg(res.resultMsg, {icon: res.icon});
	   	  				} 
	  		       }
	  		    })		   
	  		    form.render();
           }
		   //表单验证
		   form.verify({
			  fpid: function(value, item){ //上级菜单验证
					if(validate.isBlank(value))
					{
						return "请选择菜单！";
					}
			  }
			  ,fname: function(value, item){ //名称验证
					if(validate.isBlank(value))
					{
						return "请输入角色名称！";
					}
				    if(!validate.maxLength(value,50)){
				      return '角色名称长度不能超过50个字符';
				    }
			  }
			  ,fremarks: function(value, item)//描述验证
			  {
				    if(!validate.maxLength(value,200))
				    {
				      return '备注长度不能超过100个字符';
				    }
			  }
			});
		  
		  //表单提交事件
		  form.on('submit(formDemo)', function(data){
			    this.blur();//防止点回车，按钮重复点击
			  	var loadIndex = layer.load(1);//显示加载层
			  	var requestData = formToJsonObj("form_edit", []);
				delete requestData.pid;
				var rolearry=Powerfun("pid","id");//菜单列表
	    	  	var bb={
	    	  			scmsUser:requestData,
	    	  			scmsPower:rolearry
	    	  	    }
			  	$.ajax({
			           url:upUrl ,               //请求地址
			           type: "POST",              //请求类型
			           data:formatParam(bb),              //请求数据
			           timeout: 200000,                //请求超时时间(毫秒)
			           headers: setHeader(),
			           success:function(res){         //请求成功
			        	   if(res.status == 200)
			  				{
			        		   layer.msg(res.resultMsg, {icon: res.icon},function(){
									     var index = parent.layer.getFrameIndex(window.name);
									      parent.layui.table.reload("tabledata")//
									      parent.layer.close(index);//关闭弹出的子页面窗口
								}); 					
			  				}else if(res.status == 666){
			  				  TIMEOUT(); 
			  		        }else{
			  					layer.msg(res.resultMsg, {icon: res.icon});
			  				} 
			        	   layer.close(loadIndex);//关闭加载层      
		              }
		           })
				return false; //阻止表单跳转。
			});
		});
})
function permissionBeforeClick(treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj(treeId);
	zTree.checkNode(treeNode, !treeNode.checked, true, true);
	return false;
}

function permissionOnCheck(e, treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj(treeId);
	var checkedNodes = zTree.getCheckedNodes(true);
	var ids = "", names = "";
	for (var i = 0; i < checkedNodes.length; i++) {
		ids += checkedNodes[i].id;
		names += checkedNodes[i].name;
		if ((i + 1) < checkedNodes.length) {
			ids += ",";
			names += ",";
		}
	}
	$("#pid").val(ids);
	//$("#pname").val(names);
}

function showMenu() {
	var groupObj = $("#pname");
	var groupOffset = $("#pname").offset();
	$("#permissionMenuContent").css({
		left : groupOffset.left + "px",
		top : groupOffset.top + groupObj.outerHeight() + "px"
	}).slideDown("fast");

	$("body").bind("mousedown", permissionOnBodyDown);
}
function permissionHideMenu() {
	$("#permissionMenuContent").fadeOut("fast");
	$("body").unbind("mousedown", permissionOnBodyDown);
}
function permissionOnBodyDown(event) {
	if (!(event.target.id == "pname"
			|| event.target.id == "permissionMenuContent" || $(event.target)
			.parents("#permissionMenuContent").length > 0)) {
		permissionHideMenu();
	}
}


